From d4895766cb072c5275865683db920794e605bc0e Mon Sep 17 00:00:00 2001 From: Anthony Liguori Date: Mon, 12 Dec 2005 15:11:13 +0000 Subject: [PATCH] Make sure to fork again after setsid() so that child cannot regain CTTY. Signed-off-by: Anthony Liguori --- tools/console/daemon/utils.c | 6 ++++++ tools/xenstore/xenstored_core.c | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/tools/console/daemon/utils.c b/tools/console/daemon/utils.c index 372f19e3e5..524e8068dc 100644 --- a/tools/console/daemon/utils.c +++ b/tools/console/daemon/utils.c @@ -90,6 +90,12 @@ void daemonize(const char *pidfile) setsid(); + if ((pid = fork()) > 0) { + exit(0); + } else if (pid == -1) { + err(errno, "fork() failed"); + } + /* redirect fd 0,1,2 to /dev/null */ if ((fd = open("/dev/null",O_RDWR)) == -1) { exit(1); diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c index a259f3a41f..6afde0ae80 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -1491,6 +1491,13 @@ static void daemonize(void) /* Session leader so ^C doesn't whack us. */ setsid(); + + /* Let session leader exit so child cannot regain CTTY */ + if ((pid = fork()) < 0) + barf_perror("Failed to fork daemon"); + if (pid != 0) + exit(0); + #ifndef TESTING /* Relative paths for socket names */ /* Move off any mount points we might be in. */ chdir("/"); -- 2.30.2